package com.sqx.modules.health.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.modules.health.common.Constants;
import com.sqx.modules.health.common.Result;
import com.sqx.modules.health.entity.AppOrder;
import com.sqx.modules.health.entity.NcgDeviceWatch;
import com.sqx.modules.health.service.IAppOrderService;
import com.sqx.modules.health.service.INcgDeviceWatchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gxy
 * @since 2022-08-12
 */

@Api(value = "", tags = "", description = "")
@RestController
@RequestMapping("/health/appOrder")
public class AppOrderController {
    @Autowired
    private IAppOrderService appOrderService;
    @Autowired
    private INcgDeviceWatchService ncgDeviceWatchService;

    // 新增或更新
    @PostMapping("/save")
    @ApiOperation(value = "新增或更新", notes = "新增或更新")
    public Result save(@RequestBody AppOrder appOrder){
        return Result.success(appOrderService.save(appOrder));
    }
    // 新增或更新
    @PostMapping("/save2")
    @ApiOperation(value = "新增或更新", notes = "新增或更新")
    public Result save2(@RequestBody AppOrder appOrder){
        return Result.success(appOrderService.saveOrUpdate(appOrder));
    }

    // 根据ID删除
    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据ID删除", notes = "根据ID删除")
    public Result delete(@PathVariable Integer id){
        return Result.success(appOrderService.removeById(id));
    }

    // 根据IDS删除
    @PostMapping("/del/batch")
    @ApiOperation(value = "根据IDS删除", notes = "根据IDS删除")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        return Result.success(appOrderService.removeByIds(ids));
    }

    // 查询全部
    @GetMapping("/findAll")
    @ApiOperation(value = "查询全部", notes = "查询全部")
    public Result findAll(){
        return Result.success(appOrderService.list());
    }

    // 根据ID查询一条记录
    @GetMapping("/findOne")
    @ApiOperation(value = "根据ID查询一条记录", notes = "根据ID查询一条记录")
    public Result findOne(@PathVariable Integer id){
        return Result.success(appOrderService.getById(id));
    }

    // 分页查询
    @GetMapping("/findPage")
    @ApiOperation(value = "分页查询", notes = "分页查询")
    public Result findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam String hid){
        QueryWrapper<AppOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ObjectUtil.isNotEmpty(hid),"hid",hid);
        queryWrapper.orderByDesc("id");
        Page<AppOrder> result = appOrderService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return  Result.success(result);
    }

    // 租赁用户，付款完毕后，领走设备，等待归还，点击等待归还
    @GetMapping("/orderBack")
    @ApiOperation(value = "等待归还", notes = "等待归还")
    public Result orderBack(@RequestParam String orderno){

        QueryWrapper<AppOrder> wrapper = new QueryWrapper<AppOrder>()
                .eq(ObjectUtil.isNotEmpty(orderno), "orderno", orderno);
        AppOrder order =  appOrderService.getOne(wrapper);
        order.setStatus(3);//已经归还

        //hsx 租赁用户 归还设备 解绑 20220919  start 添加
        //如果订单表中被设置了具体的设备号，说明可能被租赁人使用了，点击等待归还后，就必须解绑
        if(ObjectUtil.isNotEmpty(order.getImei())){
            QueryWrapper<NcgDeviceWatch> qwrapper = new QueryWrapper<NcgDeviceWatch>()
                    .eq(ObjectUtil.isNotEmpty(order.getImei()), "imei", order.getImei());
            NcgDeviceWatch releaseDevice = ncgDeviceWatchService.getOne(qwrapper);
            releaseDevice.setUid(null);
            releaseDevice.setDoctorid(null);
            releaseDevice.setStatus(3);
            releaseDevice.setPositionid(1);//设备回归院内 变为1
            ncgDeviceWatchService.saveOrUpdate(releaseDevice);
        }
        //hsx 租赁用户 归还设备 解绑 20220919  end 添加
        if (appOrderService.saveOrUpdate(order)){
            return Result.success("设备归还成功");
        }else {
            return Result.error(Constants.CODE_600,"设备归还失败");
        }
    }
    // 订单核销
    @GetMapping("/orderFinish")
    @ApiOperation(value = "订单核销", notes = "订单核销")
    public Result orderFinish(@RequestParam String orderno){

        QueryWrapper<AppOrder> wrapper = new QueryWrapper<AppOrder>()
                .eq(ObjectUtil.isNotEmpty(orderno), "orderno", orderno);

        AppOrder order =  appOrderService.getOne(wrapper);

        order.setStatus(2);

        if (appOrderService.saveOrUpdate(order)){
            return Result.success("支付成功");
        }else {
            return Result.error(Constants.CODE_600,"支付失败");
        }
    }
}

